package com.qk.common.util;


import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.qk.common.config.AliYunOSSProperties;
import com.qk.common.dto.OSSObjectVersionDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @文件名: AliyunOSSDelFils
 * @作者: DX
 * @创建日期: 2025/10/10
 * @描述: OSS文件删除工具类
 */
@Slf4j
@Component
public class AliyunOSSDelFils {

    @Autowired
    private AliYunOSSProperties aliYunOSSOperators;
    public void batchDeleteVersions(List<OSSObjectVersionDto> objectVersions){
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = aliYunOSSOperators.getEndpoint();
        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = null;
        try {
            credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        } catch (com.aliyuncs.exceptions.ClientException e) {
            log.error(e.getMessage());
        }
        // 填写Bucket名称，例如examplebucket。
        String bucketName = aliYunOSSOperators.getBucketName();
        // 填写Bucket所在地域。以华东1（杭州）为例，Region填写为cn-hangzhou。
        String region = aliYunOSSOperators.getRegion();

        // 创建OSSClient实例。
        // 当OSSClient实例不再使用时，调用shutdown方法以释放资源。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();


        try {

            for ( OSSObjectVersionDto versionInfo : objectVersions) {
                log.info("删除文件：{}",versionInfo.getKeyName());
                ossClient.deleteVersion(bucketName, versionInfo.getKeyName(), versionInfo.getVersionId());
            }
        } catch (OSSException oe) {
            log.error("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            log.error("Error Message:" + oe.getErrorMessage());
            log.error("Error Code:" + oe.getErrorCode());
            log.error("Request ID:" + oe.getRequestId());
            log.error("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            log.error("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            log.error("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

}